const sequelize = require('../db/mysql')
const {
    DataTypes,
    Model
} = require('sequelize')

class Users extends Model {}
class Classifications extends Model {}
class Matters extends Model {}
class Orders extends Model {}
class Notes extends Model {}

Users.init({
    Id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    Name: {
        type: DataTypes.STRING
    },
    Phone: {
        type: DataTypes.STRING
    },
    Password: {
        type: DataTypes.STRING
    },
    User_id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4
    }
},{
    sequelize, // 需要连接的实例
    modelName: 'Users'
});

Classifications.init({
    Id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    Name: {
        type: DataTypes.STRING
    },
    Color: {
        type: DataTypes.STRING
    },
    Icon: {
        type: DataTypes.STRING
    },
    UserId: {
        type: DataTypes.INTEGER
    }
},{
    sequelize,
    modelName: 'Classifications'
})

Matters.init({
    Id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    Name: {
        type: DataTypes.STRING
    },
    State: {
        type: DataTypes.INTEGER
    },
    StartDate:{
        type: DataTypes.DATEONLY
    },
    EndDate:{
        type:DataTypes.DATEONLY
    },
    grade:{
        type:DataTypes.INTEGER
    },
    Describe:{
        type:DataTypes.STRING
    },
    ClassificationId: {
        type: DataTypes.INTEGER
    },
    UserId: {
        type: DataTypes.INTEGER
    }
},{
    sequelize,
    modelName:'Matters'
})

Orders.init({
    Id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    Name:{
        type:DataTypes.STRING
    },
    State: {
        type: DataTypes.INTEGER
    },
    StartDate: {
        type: DataTypes.DATE
    },
    EndDate: {
        type:DataTypes.DATE
    },
    Duration: {
        defaultValue:0,
        type:DataTypes.INTEGER
    },
    Describe:{
        type:DataTypes.STRING
    },
    UserId:{
        type:DataTypes.INTEGER
    }
},{
    sequelize,
    modelName: 'Orders'
})

Notes.init({
    Id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true
    },
    UserId:{
        type:DataTypes.INTEGER
    },
    text:{
        type:DataTypes.TEXT
    }
},{
    sequelize,
    modelName: 'Notes'
})



// 设置一对多 多对多模型
Classifications.hasMany(Matters,{
    foreignKey:'ClassificationId'
})
Matters.belongsTo(Classifications)

Matters.hasMany(Orders,{
    foreignKey:'MatterId'
})
Orders.belongsTo(Matters)

Users.hasMany(Classifications,{
    foreignKey:'UserId'
})
Classifications.belongsTo(Users)

Users.hasMany(Orders),{
    foreignKey:'UserId'
}
Orders.belongsTo(Users)

Users.hasMany(Matters),{
    foreignKey:'UserId'
}
Matters.belongsTo(Users)

Users.hasMany(Notes),{
    foreignKey:'UserId'
}
Notes.belongsTo(Users)

// 检测表格式是否改动，如相同则不更新
Users.sync()
Classifications.sync()
Matters.sync()
Orders.sync()
Notes.sync()

module.exports = {
    Users,
    Classifications,
    Matters,
    Orders,
    Notes
}







